package com.wehealth;


import com.wehealth.util.QuestionnaireDataManager;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class Questionnaire_page1 extends Fragment {

	private MainActivity context;

	private EditText   mageEt;
	private EditText   mHeightEt;
	private EditText   mWeightEt;
	private QuestionnaireDataManager  mQuestionnaireDataManage;

	public  Questionnaire_page1()
	{
		Log.i("init:", "Message"); 

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return  inflater.inflate(R.layout.questionnaire_page1, null);
	}
	
	
	
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		
		context =(MainActivity)getActivity();
		
		mQuestionnaireDataManage = QuestionnaireDataManager.getInstance();
		
		TextView title=(TextView)context.findViewById(R.id.header_title);
		String titlename = context.getString(R.string.questionnaire_title);
		title.setText(titlename);
		Button bBack = (Button) context.findViewById(R.id.header_bBack);
		bBack.setVisibility(View.GONE);
		
		mageEt =(EditText)context.findViewById(R.id.questionnaire_page1_age_et);
		mHeightEt = (EditText)context.findViewById(R.id.questionnaire_page1_height_et);
		mWeightEt =  (EditText)context.findViewById(R.id.questionnaire_page1_weight_person_et);
		
		Button questionnaire_page1_next_bt = (Button)context.findViewById(R.id.questionnaire_page1_next_bt);
		questionnaire_page1_next_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String height = mHeightEt.getText().toString().trim();
				String weight = mWeightEt.getText().toString().trim();
				Log.d("SIMMON","height="+height);
				Log.d("SIMMON","weight="+weight);
				if(height.equals("")||height==null ||weight.equals("")||weight==null){
					openDialog(0);
				}else{
				int  heightValue = Integer.valueOf(height);
				mQuestionnaireDataManage.setheight(heightValue);
				int weightValue = Integer.valueOf(weight);
				mQuestionnaireDataManage.setWeight(weightValue);
				float BMIValue = mQuestionnaireDataManage.getBMIValue();
				
				int score1 =0;
		        if(BMIValue <18.5){
		        	score1 = 60;
		        }else if(BMIValue >=18.5 && BMIValue < 25){
		        	score1 = 100;
		        }else if(BMIValue >=25 && BMIValue < 30){
		        	score1 = 50;
		        }else if(BMIValue >=30 && BMIValue <35){
		        	score1 =25;
		        }else if(BMIValue>35){
		        	score1 = 0;
		        }
		        mQuestionnaireDataManage.setScore1(score1);
				
				
				openDialog(BMIValue);
				}
			}
		});

	}

	
	private void openDialog(final float result){
		        AlertDialog.Builder builder = new AlertDialog.Builder(context); 
		        builder.setIcon(null); 
		        String BMI_result =getString(R.string.questionnaire_page1_BMI_no_value);;
		        if(result ==0){
		        	BMI_result =getString(R.string.questionnaire_page1_BMI_no_value);;
		        }else if(result <18.5){
		        	BMI_result = getString(R.string.questionnaire_page1_BMI_185);
		        }else if(result >=18.5 && result < 25){
		        	BMI_result = getString(R.string.questionnaire_page1_BMI_25);
		        }else if(result >=25 && result <32){
		        	BMI_result = getString(R.string.questionnaire_page1_BMI_32);
		        }else if(result >=32){
		        	BMI_result = getString(R.string.questionnaire_page1_BMI_32_more);
		        }
		        
		         TextView textview = new TextView(context);
		         textview.setText(BMI_result);
		         textview.setTextColor(Color.BLACK);
		         textview.setTextSize(16);
		         textview.setPadding(5, 5, 5, 5);

		         textview.setGravity(Gravity.CENTER_VERTICAL);
		         builder.setView(textview);
		         
		         
		         builder.setPositiveButton("确定", new AlertDialog.OnClickListener() {
		             @Override
		             public void onClick(DialogInterface arg0, int arg1) {
		                 // TODO Auto-generated method stub
		            	 if(result!=0){
							   Questionnaire_page2 page2 = new Questionnaire_page2();
							   context.switchContent(page2);
		            	 }
		             }
		             
		         });
		         builder.create().show();
		         
		     }

}
